package com.library.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.library.domain.pojo.Reader;
import com.library.mapper.ReaderMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : 凉小天
 * @date : 2021/6/30 10:28
 */
@Service("readerService")
public class ReaderServiceImpl extends ServiceImpl<ReaderMapper, Reader> implements ReaderService {

    /**
     * 分页查询读者
     * @param page
     * @param reader
     * @return
     */

    @Override
    public Map<String, Object> page(Page<Reader> page, Reader reader) {
        Long readerId = reader.getReaderId();
        String username = reader.getUsername();
        String phone = reader.getPhone();
        String address = reader.getAddress();

        QueryWrapper<Reader> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(!StringUtils.isEmpty(readerId),Reader::getReaderId , readerId)
                .like(!StringUtils.isEmpty(username),Reader::getUsername,username)
                .like(!StringUtils.isEmpty(phone), Reader::getPhone,phone)
                .like(!StringUtils.isEmpty(address),Reader::getAddress,address);

        Page<Reader> selectPage = this.baseMapper.selectPage(page,wrapper);
        Map<String ,Object> map = new HashMap<>();
        map.put("list",selectPage.getRecords());
        map.put("total",selectPage.getTotal());
        return map;
    }
}
